package com.feihao.zhou.Test;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class DBUtils {
    //私有构造：
    private DBUtils() {
    }

    //定义sqlSession:
    private static SqlSession sqlSession;

    //静态代码快初始化sqlSession：
    static {
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = sqlSessionFactory.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //提供一个获取sqlSession的public 静态方法：
    public static SqlSession getSqlSession() {
        return sqlSession;
    }
}
